package com.share.thread.pc;

import java.util.LinkedList;

/**
 * @desc:
 * @author:caifan
 * @date:2022/1/29
 */
public class MyBlocking<T> {
    private int maxSize;
    private LinkedList<T> list;

    public MyBlocking(int maxSize) {
        this.maxSize = maxSize;
        list = new LinkedList();
    }

    public synchronized void put(T t) throws InterruptedException {
        while (list.size() == maxSize) {
            wait();
        }
        list.add(t);
        notifyAll();
    }

    public synchronized T take() throws InterruptedException {
        while (list.size() == 0) {
            wait();
        }
        T t =  list.remove();
        notifyAll();
        return t;
    }
}
